/**
 * non_delivery.jsp 的js文件
 */

$(function(){
	$("#nondelivery_grid").datagrid({
		url:'admin/order/nondelivery?method=index',
		loadMsg:'数据加载中',
		fit:'true',
	    idField:'id',
	    toolbar:'#nondiliverOrderTool',
	    pagination:true,
	    rownumbers:true,//行号  
	    checkOnSelect : true,
		selectOnCheck : true,
		collapsible: true,
		fitColumns:true,
	    columns:[[
  	    {
			field : "ck",
			checkbox : true
		},	              
	    {
			field : "id",
			hidden : true
		},     
		
	    {
			field:'order_no',
			title:'订单编号',
			width:140,
			align:'center'
	    },    
	    {
	    	field:'customer_no',
	    	title:'客户编号',
	    	width:140,
	    	align:'center'
	     },
	     {
	    	 field:'business_type',
	    	 title:'订单业务种类',
	    	 width:150,
	    	 align:'center',
	    },
	     {
	    	 field:'postage',
	    	 title:'订单邮费',
	    	 width:100,
	    	 align:'center',
	   	        formatter: function(value,row,index){
	   	        	if(value==null){
	   	        		return "未指定";
	   	        	}else return value;
	   	        }
	    },
	     {
	    	 field:'laborFee',
	    	 title:'订单人工费',
	    	 width:100,
	    	 align:'center',
	   	        formatter: function(value,row,index){
	   	        	if(value==null){
	   	        		return "未指定";
	   	        	}else return value;
	   	        }
	    },
	     {
	    	 field:'create_tm',
	    	 title:'下单时间',
	    	 width:150,
	    	 align:'center',
   	        formatter: function(value,row,index){
   	        	return getTime(value);
   	        }
	    },
	     {
	    	 field:'state',
	    	 title:'订单状态',
	    	 width:100,
	    	 align:'center',
	    	 formatter:function(value, row, index){
	    		 if(value==0)
	    			 return "未审核";
	    		 else if(value==1)
	    			 return "审核中";
	    		 else if(value==2)
	    			 return "备货中";
	    		 else if(value == 3)
	    			 return "待付款";
	    		 else if(value==4)
	    			 return "发货中";
	    		 else if(value==5)
	    			 return "完成";
	    	 }
	    },
	     {
	    	 field:'action',
	    	 title:'订单操作',
	    	 width:100,
	    	 align:'center',
	    	 formatter:function(value, row, index){
	    		 var action = '<a href="javascript:void(0)" onclick="nonOperator(this)" class="operator-btn">订单操作</a>';
	    		 return action;
	    	 }
	    },
	    
	     {
	    	 field:'detail',
	    	 title:'查看详情',
	    	 width:100,
	    	 align:'center',
	    	 formatter:function(value, row, index){
	    		 var action = '<a href="javascript:void(0)" id="detail_btn" class="easyui-linkbutton" onclick = "showNonDetail('+row['id']+')">查看详情</a>';
	    		 return action;
	    	 }
	    }
	    ]]
	});
});

function doDelNondiliverOrder(){

	var orderNos = [];
	var rows = $("#nondelivery_grid").datagrid('getChecked');
	if(rows==null||rows.length==0)
		$.messager.alert('提示信息','请选中数据删除!');
	else {
		$.messager.confirm('确认消息', '确定删除吗?', function(r){
			if (r){

				for(var i = 0;i<rows.length;i++)
					orderNos.push(rows[i].order_no);
				console.log(orderNos);
				$.ajax({
					type:'POST',
					dataType:'json',
					url:'admin/order/all/delOrders',
					data:{"orderNos": orderNos }, 
					traditional:true,
					success:function(data){
						$.messager.alert('提示信息',data.msg);
						if(data.success){
							$('#nondelivery_grid').datagrid('load');
							$('#nondelivery_grid').datagrid("clearSelections")
						}
					}
				});
			}
		});
	}
}

function showNonDetail(id){
	$.ajax({
		type: 'GET',
		url:'admin/order/localorder?method=show&id='+id,
		dataType:'JSON',
		success:function(data){
//			$("#change_form").form('load',data);
			$("#local_show_modal #order_no").text(data.order_no);
			$("#local_show_modal #customer_no").text(data.customer_no);
			
			if(data.state==0)
				$("#local_show_modal #state").text("未审核");
			else if(data.state==1)
				$("#local_show_modal #state").text("审核中");
			else if(data.state==2)
				$("#local_show_modal #state").text("备货中");
			else if(data.state==3)
				$("#local_show_modal #state").text("待付款");		
			else if(data.state==4)
				$("#local_show_modal #state").text("发货中");
			else if(data.state==5)
				$("#local_show_modal #state").text("完成");
			

			$("#local_show_modal #length").text(data.length);
			$("#local_show_modal #width").text(data.width);
			$("#local_show_modal #height").text(data.height);
			$("#local_show_modal #weight").text(data.weight);
			$("#local_show_modal #pro_number").text(data.pro_number);
			$("#local_show_modal #pro_name").text(data.pro_name);
			$("#local_show_modal #dis_address").text(data.dis_address);
			

			$("#local_show_modal #postage").text(data.postage==null?"未指定":data.postage);
			$("#local_show_modal #laborFee").text(data.laborFee==null?"未指定":data.laborFee);
			$("#local_show_modal #warehouse").text(data.warehouse);
			$("#local_show_modal #location").text(data.location);
			$("#local_show_modal #loc_no").text(data.loc_no);
			if(data.image_name!=null&&data.image_name!=""){
				$("#local_show_modal #image_id").text('点击下载');
				$("#local_show_modal #image_id").attr('href',data.image_name);
			}
				
			else {
				$("#local_show_modal #image_id").text('未上传');
				$("#local_show_modal #image_id").attr('href',null);
			}
			
			$("#local_show_modal #business_type").text(data.business_type);				
			$("#local_show_modal #create_tm").text(getTime(data.create_tm));
			
			$("#local_show_modal").window('open');
		},
        error: function(err) { 
        	alert("错误！"+err);   
        }   
	});
}

function nonOperator(target){
	var rows = $("#nondelivery_grid").datagrid('getRows');
	var state = rows[getRowIndex(target)].state;
	
	var row = rows[getRowIndex(target)];
	if(row.state==0)
		row.state=1;
	$("#non_local_operator_form").form('load',row);
	$("#non_local_operator_modal").window('open');
}

$("#non_local_operator_confirm_btn").click(function(){
//	var state = $("input[type=radio][name=state]:checked").val();
	console.log("in");
	$("#non_local_operator_form").form('submit',{
		url:'admin/order/localorder?method=update',
		dataType:'json',
		onSubmit : function() {
			var isValid = $(this).form('enableValidation').form("validate");
			return isValid;
		},
		success:function(data){
			
			var data = eval("(" + data + ")");
			$.messager.alert("提示信息",data.msg);
			$("#non_local_operator_modal").window('close');
			$("#nondelivery_grid").datagrid('load');
		},
	    error: function(err) { 
	    	alert("错误！"+err);   
	    }
	});
});
$.extend($.fn.validatebox.defaults.rules, {
    intOrFloat : {// 验证整数或小数
        validator : function(value) {
            return /^\d+(\.\d+)?$/i.test(value);
        },
        message : '请输入数字，并确保格式正确'
    }
});

function getRowIndex(target){
	var tr = $(target).closest('tr.datagrid-row');
	return parseInt(tr.attr('datagrid-row-index'));
}

function getTime(old) {
	var date = new Date(old);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	if (month < 10)
		month = "0" + month;
	var day = date.getDate();
	if (day < 10)
		day = "0" + day;
	var hour = date.getHours();
	if (hour < 10)
		hour = "0" + hour;
	var minute = date.getMinutes();
	if (minute < 10)
		minute = "0" + minute;
	var seconds = date.getSeconds();
	if (seconds < 10)
		seconds = "0" + seconds;
	var newDate = year + "-" + month + "-" + day + " " + hour + ":" + minute
			+ ":" + seconds;
	return newDate;
}


